/*******************************************************************************
*
* (c) Copyright 2014 Freescale Semiconductor
*
****************************************************************************//*!
*
* @file     SIM.c
*
* @author   Salvador Zendejas
*
* @version  0.0.1
*
* @date     Aug 22, 2014
*
* @brief    System Integration Module configuration for Kinetis K.
*
*******************************************************************************/

/*******************************************************************************
* Includes
*******************************************************************************/
#include "SIM.h" /* Include peripheral declarations */
#include "../typedef.h" /* Include typedefs declarations */

/*******************************************************************************
* Global variables
*******************************************************************************/
void vfn_SIM_Init(void){
	sMCU_Info_t sSysDeviceInfo = tfn_GetInfoDeviceID();
	(void)sSysDeviceInfo;
	SIM_SOPT1 = SIM_SOPT1_OSC32KSEL(e_OSC32KCLK);
	
#if _DEBUG_ && _CLKOUT_
	SIM_SOPT2 = SIM_SOPT2_CLKOUTSEL(e_MCGIRCLK);
#endif
/**Config the System clock Gating**/
#if MK64FN1M0VLL12
	SIM_SCGC1 = (UART4 << SIM_SCGC1_UART4_SHIFT) | (UART5 << SIM_SCGC1_UART5_SHIFT) | (I2C2 << SIM_SCGC1_I2C2_SHIFT);
	SIM_SCGC2 = (DAC1 << SIM_SCGC2_DAC1_SHIFT) | (DAC0 << SIM_SCGC2_DAC0_SHIFT) | (ENET << SIM_SCGC2_ENET_SHIFT);
	SIM_SCGC3 = (ADC1 << SIM_SCGC3_ADC1_SHIFT) | (FTM3 << SIM_SCGC3_FTM3_SHIFT) | (FTM2 << SIM_SCGC3_FTM2_SHIFT) \
			| (SDHC << SIM_SCGC3_SDHC_SHIFT) | (SPI2 << SIM_SCGC3_SPI2_SHIFT) | (RNGA << SIM_SCGC3_RNGA_SHIFT);
	SIM_SCGC4 = (VREF << SIM_SCGC4_VREF_SHIFT) | (CMP << SIM_SCGC4_CMP_SHIFT) | (USBOTG << SIM_SCGC4_USBOTG_SHIFT) \
				| (UART3 << SIM_SCGC4_UART3_SHIFT) | (UART2 << SIM_SCGC4_UART2_SHIFT) | (UART1 << SIM_SCGC4_UART1_SHIFT) | (UART0 << SIM_SCGC4_UART0_SHIFT) \
				| (I2C1 << SIM_SCGC4_I2C1_SHIFT) | (I2C0 << SIM_SCGC4_I2C0_SHIFT) | (CMT << SIM_SCGC4_CMT_SHIFT) | (EWM << SIM_SCGC4_EWM_SHIFT);
	SIM_SCGC5 = (PORTE << SIM_SCGC5_PORTE_SHIFT) | (PORTD << SIM_SCGC5_PORTD_SHIFT) | (PORTC << SIM_SCGC5_PORTC_SHIFT) \
				| (PORTB << SIM_SCGC5_PORTB_SHIFT) | (PORTA << SIM_SCGC5_PORTA_SHIFT) | (LPTMR << SIM_SCGC5_LPTMR_SHIFT);
	/*there is missing the SIM_SCGC6 & SIM_SCGC7 */
#endif
}

/*******************************************************************************
* Local variables
*******************************************************************************/
//static volatile     uint8_t temp;

/*******************************************************************************
* Constants
*******************************************************************************/


/*******************************************************************************
* Global functions
*******************************************************************************/



/*******************************************************************************
* Local functions (static)
*******************************************************************************/

static sMCU_Info_t tfn_GetInfoDeviceID(void){
	sMCU_Info_t sSysDeviceInfo;
	uint8_t x;
	for (x = 0; x < _K7x_Fam; ++x) {
		if ((SIM_SDID & SIM_SDID_FAMILYID_MASK) >> SIM_SDID_FAMILYID_SHIFT == x)	sSysDeviceInfo.eFAMILYID = (eKinetisFam_t)x;
	}
	
	for (x = 0; x < _Kx6_Subfam; x++) {
			if ((SIM_SDID & SIM_SDID_SUBFAMID_MASK) >> SIM_SDID_SUBFAMID_SHIFT == x)	sSysDeviceInfo.eSUBFAMID = (eKinetisSubFam_t)x;
		}
	
	for (x = 0; x < _V_Series; x++) {
				if ((SIM_SDID & SIM_SDID_SERIESID_MASK) >> SIM_SDID_SERIESID_SHIFT == x)	sSysDeviceInfo.eSERIESID = (eKinetisSeriesID_t)x;
			}
	
	for (x = 0; x < _256Pin; x++) {
					if ((SIM_SDID & SIM_SDID_PINID_MASK) >> SIM_SDID_PINID_SHIFT == x)	sSysDeviceInfo.ePINID = (ePIN_ID_t)x;
				}
	
	sSysDeviceInfo.u8REVID = (uint8_t)((SIM_SDID & SIM_SDID_REVID_MASK) >> SIM_SDID_REVID_SHIFT);
	return sSysDeviceInfo;
}

/*******************************************************************************
* ISR functions
*******************************************************************************/

